package com.huazi.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.huazi.eduservice.entity.EduSubject;
import com.huazi.eduservice.listener.SubjectDataListener;
import com.huazi.eduservice.mapper.EduSubjectMapper;
import com.huazi.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huazi.eduservice.vo.SubjectData;
import com.huazi.eduservice.vo.SubjectInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huazi
 * @since 2021-03-17
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(@RequestBody(required = false) MultipartFile file, EduSubjectService subjectService) {
        try{
            //文件输入流
            InputStream is=file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(is, SubjectData.class,
                    new SubjectDataListener(subjectService))
                    .sheet().doRead();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

    //获取所有2级课程类别
    @Override
    public List<SubjectInfo> getSubjectInfo() {
        return baseMapper.getSubjectInfo();
    }
}
